package cn.com.nes.site.service.crm.opportunity.impl;

import cn.com.nes.mybatis.agent.crm.entity.OpportunityDeviceModel;
import cn.com.nes.mybatis.agent.crm.mapper.OpportunityDeviceModelMapper;
import cn.com.nes.site.entity.em.DeleteEnum;
import cn.com.nes.site.service.crm.opportunity.OpportunityDeviceModelService;
import cn.com.nes.site.service.crm.opportunity.bo.OpportunityDeviceModelBo;
import cn.com.nes.site.service.crm.opportunity.dto.OpportunityDeviceModelDTO;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class OpportunityDeviceModelServiceImpl extends ServiceImpl<OpportunityDeviceModelMapper, OpportunityDeviceModel> implements OpportunityDeviceModelService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveBatch(OpportunityDeviceModelBo opportunityDeviceModelBo) throws Exception {

        deleteByOpportunity(opportunityDeviceModelBo);

        if (CollUtil.isNotEmpty(opportunityDeviceModelBo.getDeviceModels())) {
            log.debug("save opportunityDeviceModelBo:{}", opportunityDeviceModelBo);
            List<OpportunityDeviceModel> list = new ArrayList<>();
            for (OpportunityDeviceModelDTO deviceModel : opportunityDeviceModelBo.getDeviceModels()) {
                OpportunityDeviceModel opportunityDeviceModel = new OpportunityDeviceModel();
                opportunityDeviceModel.setDeviceModel(deviceModel.getDeviceModel());
                opportunityDeviceModel.setDeviceModelName(deviceModel.getDeviceModelName());
                opportunityDeviceModel.setEnergyType(deviceModel.getEnergyType());
                opportunityDeviceModel.setEnergyTypeName(deviceModel.getEnergyTypeName());
                opportunityDeviceModel.setBrandType(deviceModel.getBrandType());
                opportunityDeviceModel.setBrandTypeName(deviceModel.getBrandTypeName());
                opportunityDeviceModel.setOpportunityUuid(opportunityDeviceModelBo.getOpportunityUuid());
                opportunityDeviceModel.setProjectUuid(opportunityDeviceModelBo.getProjectUuid());
                opportunityDeviceModel.setDeleted(DeleteEnum._0.getKey());
                opportunityDeviceModel.setGmtCreate(opportunityDeviceModelBo.getOperateDate());
                opportunityDeviceModel.setGmtCreateId(opportunityDeviceModelBo.getOperateUserId());
                opportunityDeviceModel.setGmtModified(opportunityDeviceModelBo.getOperateDate());
                opportunityDeviceModel.setGmtModifiedId(opportunityDeviceModelBo.getOperateUserId());
                list.add(opportunityDeviceModel);
            }
            this.saveBatch(list);
        }
    }

    private void deleteByOpportunity(OpportunityDeviceModelBo opportunityDeviceModelBo) {
        log.debug("delete opportunityDeviceModelBo:{}", opportunityDeviceModelBo);
        this.baseMapper.deleteByOpportunity(opportunityDeviceModelBo);
    }

    /**
     * 查询设备型号范围
     * @param opportunityUuid 商务项目UUID
     * @return
     */
    @Override
    public List<OpportunityDeviceModel> getDeviceModels(String opportunityUuid) throws Exception {
        return this.baseMapper.getDeviceModels(opportunityUuid);
    }

}




